{
 "cells": [
  {
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-13T12:09:10.125733Z",
     "start_time": "2025-01-13T12:09:09.751397Z"
    }
   },
   "outputs": [],
   "execution_count": 1
  },
  {
   "cell_type": "code",
   "source": [
    "dict_data = {'A': 1,\n",
    "             'B': pd.Timestamp('20190926'),\n",
    "             'C': pd.Series(1, index=list(range(4)),dtype='float32'),\n",
    "             'D': np.array([1,2,3,4],dtype='int32'),\n",
    "             'E': [\"Python\",\"Java\",\"C++\",\"C\"],\n",
    "             'F': 'wangdao' }\n",
    "df_obj2 = pd.DataFrame(dict_data)\n",
    "print(df_obj2.index)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-13T12:09:43.467662Z",
     "start_time": "2025-01-13T12:09:43.462833Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index([0, 1, 2, 3], dtype='int64')\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "cell_type": "code",
   "source": [
    "ser_obj = pd.Series(range(5), index = list(\"abcde\"))\n",
    "print(ser_obj)\n",
    "ser_obj.index"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-13T12:11:41.922363Z",
     "start_time": "2025-01-13T12:11:41.915524Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Index(['a', 'b', 'c', 'd', 'e'], dtype='object')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T12:17:35.953559Z",
     "start_time": "2025-01-13T12:17:35.950656Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(ser_obj.loc['b']) \n",
    "print(ser_obj.iloc[2])"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "cell_type": "code",
   "source": [
    "print(ser_obj.iloc[1:3])\n",
    "print(ser_obj.loc['b':'d']) "
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-13T12:17:46.192263Z",
     "start_time": "2025-01-13T12:17:46.187374Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b    1\n",
      "c    2\n",
      "dtype: int64\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "cell_type": "code",
   "source": [
    "print(ser_obj.iloc[[0, 2, 4]])\n",
    "print(ser_obj.loc[['a', 'e']])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-13T12:18:44.568391Z",
     "start_time": "2025-01-13T12:18:44.562739Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "c    2\n",
      "e    4\n",
      "dtype: int64\n",
      "a    0\n",
      "e    4\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "cell_type": "code",
   "source": [
    "ser_bool = ser_obj > 2\n",
    "print(ser_obj)\n",
    "print(ser_bool)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-13T12:22:29.825246Z",
     "start_time": "2025-01-13T12:22:29.820695Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "a    False\n",
      "b    False\n",
      "c    False\n",
      "d     True\n",
      "e     True\n",
      "dtype: bool\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T12:22:44.467260Z",
     "start_time": "2025-01-13T12:22:44.463546Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(ser_obj[ser_bool])\n",
    "print(ser_obj[ser_obj > 2])"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "cell_type": "code",
   "source": [
    "df_obj = pd.DataFrame(np.random.randn(5,4),\n",
    "                      columns = ['a', 'b', 'c', 'd'])\n",
    "print(df_obj.head())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-13T12:25:05.980002Z",
     "start_time": "2025-01-13T12:25:05.975442Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          a         b         c         d\n",
      "0 -1.004203  1.055390 -1.062914  0.224555\n",
      "1 -0.580237 -0.513962 -2.189773  0.136373\n",
      "2  1.365181 -1.439212 -0.458307  0.025892\n",
      "3 -0.350134 -0.345796 -0.000624 -0.137859\n",
      "4 -0.106508  0.199115 -1.129174 -1.384453\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "cell_type": "code",
   "source": [
    "print(df_obj['a'])\n",
    "print('-'*50)\n",
    "print(df_obj[['a']])\n",
    "print('-'*50)\n",
    "print(type(df_obj[['a']]))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-13T12:27:29.936191Z",
     "start_time": "2025-01-13T12:27:29.930184Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0   -1.004203\n",
      "1   -0.580237\n",
      "2    1.365181\n",
      "3   -0.350134\n",
      "4   -0.106508\n",
      "Name: a, dtype: float64\n",
      "--------------------------------------------------\n",
      "          a\n",
      "0 -1.004203\n",
      "1 -0.580237\n",
      "2  1.365181\n",
      "3 -0.350134\n",
      "4 -0.106508\n",
      "--------------------------------------------------\n",
      "<class 'pandas.core.frame.DataFrame'>\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "cell_type": "code",
   "source": [
    "df_obj = pd.DataFrame(np.random.randn(5,4),\n",
    "                      columns = list('abcd'),\n",
    "                      index=list('abcde'))\n",
    "print(df_obj)\n",
    "print('-'*50)\n",
    "print(df_obj['a']) \n",
    "print('-'*50)\n",
    "print(df_obj.loc['a'])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-13T12:29:36.260788Z",
     "start_time": "2025-01-13T12:29:36.255984Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          a         b         c         d\n",
      "a  0.260667 -1.137828  0.222201  1.398596\n",
      "b -0.978445  0.400535  0.517030  1.104074\n",
      "c -2.477088  0.205745  0.936373 -1.606462\n",
      "d -0.536797 -1.391960  0.051392  0.039178\n",
      "e  1.035316  0.954769  0.568685  0.508632\n",
      "--------------------------------------------------\n",
      "a    0.260667\n",
      "b   -0.978445\n",
      "c   -2.477088\n",
      "d   -0.536797\n",
      "e    1.035316\n",
      "Name: a, dtype: float64\n",
      "--------------------------------------------------\n",
      "a    0.260667\n",
      "b   -1.137828\n",
      "c    0.222201\n",
      "d    1.398596\n",
      "Name: a, dtype: float64\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T12:31:16.204948Z",
     "start_time": "2025-01-13T12:31:16.197934Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(df_obj.loc['a':'c', 'b':'d']) \n",
    "print(df_obj.loc[['a','c'], ['b','d']]) \n",
    "print(df_obj.loc[['c'],['b']]) \n",
    "print(df_obj.loc['c','b']) "
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          b         c         d\n",
      "a -1.137828  0.222201  1.398596\n",
      "b  0.400535  0.517030  1.104074\n",
      "c  0.205745  0.936373 -1.606462\n",
      "          b         d\n",
      "a -1.137828  1.398596\n",
      "c  0.205745 -1.606462\n",
      "          b\n",
      "c  0.205745\n",
      "0.20574505566917448\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "cell_type": "code",
   "source": "ser_obj",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-13T12:34:51.413041Z",
     "start_time": "2025-01-13T12:34:51.409231Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    0\n",
       "b    1\n",
       "c    2\n",
       "d    3\n",
       "e    4\n",
       "dtype: int64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 14
  },
  {
   "cell_type": "code",
   "source": [
    "df_obj"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-13T12:34:59.963016Z",
     "start_time": "2025-01-13T12:34:59.956928Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "          a         b         c         d\n",
       "a  0.260667 -1.137828  0.222201  1.398596\n",
       "b -0.978445  0.400535  0.517030  1.104074\n",
       "c -2.477088  0.205745  0.936373 -1.606462\n",
       "d -0.536797 -1.391960  0.051392  0.039178\n",
       "e  1.035316  0.954769  0.568685  0.508632"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0.260667</td>\n",
       "      <td>-1.137828</td>\n",
       "      <td>0.222201</td>\n",
       "      <td>1.398596</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>-0.978445</td>\n",
       "      <td>0.400535</td>\n",
       "      <td>0.517030</td>\n",
       "      <td>1.104074</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>-2.477088</td>\n",
       "      <td>0.205745</td>\n",
       "      <td>0.936373</td>\n",
       "      <td>-1.606462</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>-0.536797</td>\n",
       "      <td>-1.391960</td>\n",
       "      <td>0.051392</td>\n",
       "      <td>0.039178</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>e</th>\n",
       "      <td>1.035316</td>\n",
       "      <td>0.954769</td>\n",
       "      <td>0.568685</td>\n",
       "      <td>0.508632</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 15
  },
  {
   "cell_type": "code",
   "source": [
    "print(df_obj.iloc[0:2, 0:2]) \n",
    "print('-'*50)\n",
    "print(df_obj.iloc[[0,2], [0,2]]) \n",
    "print('-'*50)\n",
    "print(df_obj.iloc[0,0])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-13T12:36:10.456569Z",
     "start_time": "2025-01-13T12:36:10.451336Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          a         b\n",
      "a  0.260667 -1.137828\n",
      "b -0.978445  0.400535\n",
      "--------------------------------------------------\n",
      "          a         c\n",
      "a  0.260667  0.222201\n",
      "c -2.477088  0.936373\n",
      "--------------------------------------------------\n",
      "0.2606674507883174\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "cell_type": "code",
   "source": [
    "df_obj2 = pd.DataFrame(np.random.randn(5,4))\n",
    "print(df_obj2)\n",
    "print('-'*50)\n",
    "print(df_obj2.iloc[0:2])\n",
    "print('-'*50)\n",
    "print(df_obj2.loc[0:2]) "
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-13T12:39:44.728460Z",
     "start_time": "2025-01-13T12:39:44.721452Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2         3\n",
      "0  0.631311 -1.311134 -1.308701 -0.185128\n",
      "1 -0.567692  0.890620  2.579596  1.476381\n",
      "2 -1.044420  0.534346  0.896615 -1.932489\n",
      "3 -0.891266 -0.410695  0.042818  1.587698\n",
      "4  0.580192 -1.346006  0.941089 -0.678414\n",
      "--------------------------------------------------\n",
      "          0         1         2         3\n",
      "0  0.631311 -1.311134 -1.308701 -0.185128\n",
      "1 -0.567692  0.890620  2.579596  1.476381\n",
      "--------------------------------------------------\n",
      "          0         1         2         3\n",
      "0  0.631311 -1.311134 -1.308701 -0.185128\n",
      "1 -0.567692  0.890620  2.579596  1.476381\n",
      "2 -1.044420  0.534346  0.896615 -1.932489\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "cell_type": "code",
   "source": [
    "s1 = pd.Series(range(10, 20), index = range(10))\n",
    "s2 = pd.Series(range(20, 25), index = range(5))\n",
    "print('s1+s2: ')\n",
    "s3=s1+s2\n",
    "print(s3)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-13T13:12:17.795690Z",
     "start_time": "2025-01-13T13:12:17.790678Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "s1+s2: \n",
      "0    30.0\n",
      "1    32.0\n",
      "2    34.0\n",
      "3    36.0\n",
      "4    38.0\n",
      "5     NaN\n",
      "6     NaN\n",
      "7     NaN\n",
      "8     NaN\n",
      "9     NaN\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "cell_type": "code",
   "source": [
    "a1 = np.array([1,2,3,4,5])\n",
    "a2 = np.array([1]) \n",
    "print(a2.shape)\n",
    "print(a1+a2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-13T13:14:08.465071Z",
     "start_time": "2025-01-13T13:14:08.461609Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1,)\n",
      "[2 3 4 5 6]\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T06:43:17.445141Z",
     "start_time": "2025-01-07T06:43:17.441719Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(s2)\n",
    "s1"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    20\n",
      "1    21\n",
      "2    22\n",
      "3    23\n",
      "4    24\n",
      "dtype: int64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0    10\n",
       "1    11\n",
       "2    12\n",
       "3    13\n",
       "4    14\n",
       "5    15\n",
       "6    16\n",
       "7    17\n",
       "8    18\n",
       "9    19\n",
       "dtype: int64"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 26
  },
  {
   "cell_type": "code",
   "source": [
    "print(np.isnan(s3[6]))\n",
    "print('-'*50)\n",
    "print(s2.add(s1, fill_value = 0))\n",
    "print(s2.sub(s1, fill_value = 0))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-13T13:19:51.480918Z",
     "start_time": "2025-01-13T13:19:51.475653Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "--------------------------------------------------\n",
      "0    30.0\n",
      "1    32.0\n",
      "2    34.0\n",
      "3    36.0\n",
      "4    38.0\n",
      "5    15.0\n",
      "6    16.0\n",
      "7    17.0\n",
      "8    18.0\n",
      "9    19.0\n",
      "dtype: float64\n",
      "0    10.0\n",
      "1    10.0\n",
      "2    10.0\n",
      "3    10.0\n",
      "4    10.0\n",
      "5   -15.0\n",
      "6   -16.0\n",
      "7   -17.0\n",
      "8   -18.0\n",
      "9   -19.0\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "cell_type": "code",
   "source": [
    "df1 = pd.DataFrame(np.ones((2,2)), columns = ['a', 'b'])\n",
    "df2 = pd.DataFrame(np.ones((3,3)), columns = ['a', 'b', 'c'])\n",
    "print(df1)\n",
    "print('-'*50)\n",
    "print(df2)\n",
    "print('-'*50)\n",
    "print(df2.dtypes)\n",
    "print('-'*50)\n",
    "print(df1-df2)\n",
    "print('-'*50)\n",
    "print(df2.sub(df1, fill_value = 2))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-13T13:55:11.405035Z",
     "start_time": "2025-01-13T13:55:11.395983Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     a    b\n",
      "0  1.0  1.0\n",
      "1  1.0  1.0\n",
      "--------------------------------------------------\n",
      "     a    b    c\n",
      "0  1.0  1.0  1.0\n",
      "1  1.0  1.0  1.0\n",
      "2  1.0  1.0  1.0\n",
      "--------------------------------------------------\n",
      "a    float64\n",
      "b    float64\n",
      "c    float64\n",
      "dtype: object\n",
      "--------------------------------------------------\n",
      "     a    b   c\n",
      "0  0.0  0.0 NaN\n",
      "1  0.0  0.0 NaN\n",
      "2  NaN  NaN NaN\n",
      "--------------------------------------------------\n",
      "     a    b    c\n",
      "0  0.0  0.0 -1.0\n",
      "1  0.0  0.0 -1.0\n",
      "2 -1.0 -1.0 -1.0\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": ""
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
